class HlAudio {
	context = null;
	loginUrl = `https://xiaohongpao.top/music/hometown_background_voice.mp3`
	login02 = `https://xiaohongpao.top/music/login-02.wav`
	gameUrl = `https://xiaohongpao.top/music/login_background_voice.mp3`
	menuUrl = `https://xiaohongpao.top/music/menu-selection-click.wav`
	hall = `https://xiaohongpao.top/music/hall.mp3`
	ready = `https://xiaohongpao.top/music/ready.wav`
	stepping = `https://xiaohongpao.top/music/stepping_loop.mp3`
	constructor(action, loop = false, autoPlay = false) {
		if (this.context) {
			this.context = null;
		}
		if (!action) {
			throw new Error("参数不正确")
		}
		this.action = action
		this.loop = loop;
		this.autoPlay = autoPlay;
		this.initContext()
		return this
	}

	initContext = () => {
		this.context = uni.createInnerAudioContext();
		//默认paused为真
		this.context.autoplay = this.autoPlay
		this.context.loop = this.loop
		switch (this.action) {
			case 'menu':
				this.context.src = this.menuUrl
				break;
			case 'login':
				this.context.src = this.loginUrl
				break;
			case 'login02':
				this.context.src = this.login02
				break;
			case 'game':
				this.context.src = this.gameUrl
				break;
			case 'hall':
				this.context.src = this.hall
				break;
			case 'ready':
				this.context.src = this.ready
				break;
			case 'stepping':
				this.context.src = this.stepping
				break;
			default:
				console.log("音乐地址无效")
				throw new Error("音乐地址无效")
				break;
		}

		return this;
	}

	play = () => {
		if (this.context) {
			this.context.play();
		} else {
			throw new Error("context不存在")
		}
	}

	stop = () => {
		if (this.context) {
			this.context.stop();
		}
	}

	pause = () => {
		if (this.context) {
			this.context.pause();
		}
	}

	destroy = () => {
		if (this.context) {
			this.context.destroy();
			this.context = null;
		}
	}
}

function createHlAudio(action, loop, autoPlay) {
	return new HlAudio(action, loop, autoPlay)
}

let $hl = {
	createHlAudio,
}

export {
	$hl
}